package com.example.linuxchat;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.linuxchat.adapter.LoginBack;
import com.example.linuxchat.bean.User;
import com.tencent.tauth.Tencent;

public class MainActivity extends AppCompatActivity {
    private Button btn_login;
    public static String userjson;
    private Tencent tencent=null;
    private LoginBack back=null;
    public static User user=null;
    public static MainActivity the;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        the=this;
        //实例化
        tencent=Tencent.createInstance("101886268",getApplicationContext());
        back= new LoginBack(tencent);   //QQ登入授权回调
        initView();
    }

    private void initView(){
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginIn();
            }
        });
    }
    //登入
    protected  void loginIn(){
        if(!tencent.isSessionValid()){
            tencent.login(MainActivity.this,"all",back);
        }
    }
    //注销
    protected void loginOut(){
        tencent.logout(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        tencent.onActivityResultData(requestCode,resultCode,data,back);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginOut();
    }
}
